package com.dianping.cat.alarm.rule.transform;

import com.dianping.cat.alarm.rule.IEntity;
import com.dianping.cat.alarm.rule.entity.Condition;
import com.dianping.cat.alarm.rule.entity.Config;
import com.dianping.cat.alarm.rule.entity.MetricItem;
import com.dianping.cat.alarm.rule.entity.MonitorRules;
import com.dianping.cat.alarm.rule.entity.Rule;
import com.dianping.cat.alarm.rule.entity.SubCondition;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;














public class DefaultJsonParser<T>
{
  private DefaultLinker m_linker;
  private Stack<String> m_tags;
  private Stack<Object> m_objs;
  private List<T> m_entities;
  private Class<?> m_entityClass;
  
  private DefaultJsonParser(Class<?> entityClass) {
/*  42 */     this.m_linker = new DefaultLinker(true);

/*  44 */     this.m_tags = new Stack();

/*  46 */     this.m_objs = new Stack();

/*  48 */     this.m_entities = new ArrayList();




/*  53 */     this.m_entityClass = entityClass;
  }

  
/*  57 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, InputStream in) throws IOException { return (T)parse(entityClass, new InputStreamReader(in, "utf-8")); }


  
  public static <T extends IEntity<T>> T parse(Class<T> entityClass, Reader reader) throws IOException {
/*  62 */     DefaultJsonParser parser = new DefaultJsonParser(entityClass);

/*  64 */     parser.onArrayBegin();
/*  65 */     parser.parse(new JsonReader(reader));
/*  66 */     parser.onArrayEnd();

/*  68 */     if (parser.m_entities.isEmpty()) {
/*  69 */       return null;
    }
/*  71 */     return (T)(IEntity)parser.m_entities.get(0);
  }


  
/*  76 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, String json) throws IOException { return (T)parse(entityClass, new StringReader(json)); }



/*  80 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, InputStream in) throws Exception { return parseArray(entityClass, new InputStreamReader(in, "utf-8")); }


  
  public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, Reader reader) throws Exception {
/*  85 */     DefaultJsonParser parser = new DefaultJsonParser(entityClass);

/*  87 */     parser.parse(new JsonReader(reader));
/*  88 */     return (List<T>) parser.m_entities;
  }

  
/*  92 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, String json) throws Exception { return parseArray(entityClass, new StringReader(json)); }



  
  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/*  98 */     if (value == null || value.length() == 0) {
/*  99 */       return defaultValue;
    }
    
/* 102 */     if (type == Boolean.class)
/* 103 */       return (T)Boolean.valueOf(value); 
/* 104 */     if (type == Integer.class)
/* 105 */       return (T)Integer.valueOf(value); 
/* 106 */     if (type == Long.class)
/* 107 */       return (T)Long.valueOf(value); 
/* 108 */     if (type == Short.class)
/* 109 */       return (T)Short.valueOf(value); 
/* 110 */     if (type == Float.class)
/* 111 */       return (T)Float.valueOf(value); 
/* 112 */     if (type == Double.class)
/* 113 */       return (T)Double.valueOf(value); 
/* 114 */     if (type == Byte.class)
/* 115 */       return (T)Byte.valueOf(value); 
/* 116 */     if (type == Character.class) {
/* 117 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 119 */     return (T)value;
  }

  
  private Object createRootEntity() {
    try {
/* 125 */       return this.m_entityClass.newInstance();
    
    }
/* 128 */     catch (Exception e) {
/* 129 */       throw new RuntimeException(String.format("Unable to create entity(%s) instance!", new Object[] { this.m_entityClass.getName() }), e);
    }
  }

  
/* 134 */   private boolean isTopLevel() { return (this.m_objs.size() == 1); }

  
  protected void onArrayBegin() {
/* 138 */     if (this.m_objs.isEmpty()) {
/* 139 */       this.m_objs.push(this.m_entities);
/* 140 */       this.m_tags.push("");
    } else {
/* 142 */       Object parent = this.m_objs.peek();
/* 143 */       String tag = (String)this.m_tags.peek();

/* 145 */       if (parent instanceof Rule) {
/* 146 */         if ("metric-items".equals(tag)) {
/* 147 */           this.m_objs.push(parent);
/* 148 */         } else if ("configs".equals(tag)) {
/* 149 */           this.m_objs.push(parent);
        } else {
/* 151 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
/* 153 */       } else if (parent instanceof Config) {
/* 154 */         if ("conditions".equals(tag)) {
/* 155 */           this.m_objs.push(parent);
        } else {
/* 157 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
/* 159 */       } else if (parent instanceof Condition) {
/* 160 */         if ("sub-conditions".equals(tag)) {
/* 161 */           this.m_objs.push(parent);
        } else {
/* 163 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
      } else {
/* 166 */         throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
      }
    }
  }
  protected void onArrayEnd() {
/* 171 */     this.m_objs.pop();
/* 172 */     this.m_tags.pop();
  }

  
/* 176 */   protected void onName(String name) { this.m_tags.push(name); }

  
  protected void onObjectBegin() {
/* 180 */     if (isTopLevel()) {
/* 181 */       this.m_objs.push(createRootEntity());
/* 182 */       this.m_tags.push("");
    } else {
/* 184 */       Object parent = this.m_objs.peek();
/* 185 */       String tag = (String)this.m_tags.peek();

/* 187 */       if (parent instanceof MonitorRules) {
/* 188 */         if ("rules".equals(tag)) {
/* 189 */           this.m_objs.push(parent);
        } else {
/* 191 */           String parentTag = (this.m_tags.size() >= 2) ? (String)this.m_tags.get(this.m_tags.size() - 2) : null;

/* 193 */           if ("rules".equals(parentTag)) {
/* 194 */             Rule rules = new Rule();

/* 196 */             this.m_linker.onRule((MonitorRules)parent, rules);
/* 197 */             this.m_objs.push(rules);
          } else {
/* 199 */             throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
          }
        }
/* 202 */       } else if (parent instanceof Rule) {
/* 203 */         if ("metric-items".equals(tag)) {
/* 204 */           MetricItem metricItems = new MetricItem();

/* 206 */           this.m_linker.onMetricItem((Rule)parent, metricItems);
/* 207 */           this.m_objs.push(metricItems);
/* 208 */           this.m_tags.push("");
/* 209 */         } else if ("configs".equals(tag)) {
/* 210 */           Config configs = new Config();

/* 212 */           this.m_linker.onConfig((Rule)parent, configs);
/* 213 */           this.m_objs.push(configs);
/* 214 */           this.m_tags.push("");
        } else {
/* 216 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
/* 218 */       } else if (parent instanceof Config) {
/* 219 */         if ("conditions".equals(tag)) {
/* 220 */           Condition conditions = new Condition();

/* 222 */           this.m_linker.onCondition((Config)parent, conditions);
/* 223 */           this.m_objs.push(conditions);
/* 224 */           this.m_tags.push("");
        } else {
/* 226 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
/* 228 */       } else if (parent instanceof Condition) {
/* 229 */         if ("sub-conditions".equals(tag)) {
/* 230 */           SubCondition subConditions = new SubCondition();

/* 232 */           this.m_linker.onSubCondition((Condition)parent, subConditions);
/* 233 */           this.m_objs.push(subConditions);
/* 234 */           this.m_tags.push("");
        } else {
/* 236 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
      } else {
/* 239 */         throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
      }
    }
  }
  
  protected void onObjectEnd() {
/* 245 */     this.m_tags.pop();

/* 247 */     Object entity = this.m_objs.pop();

/* 249 */     if (isTopLevel()) {
/* 250 */       this.m_entities.add((T) entity);
    }
  }
  
  protected void onValue(String value) {
/* 255 */     Object parent = this.m_objs.peek();
/* 256 */     String tag = (String)this.m_tags.pop();

/* 258 */     if (parent instanceof MonitorRules) {
/* 259 */       parseForMonitorRules((MonitorRules)parent, tag, value);
/* 260 */     } else if (parent instanceof Rule) {
/* 261 */       parseForRule((Rule)parent, tag, value);
/* 262 */     } else if (parent instanceof MetricItem) {
/* 263 */       parseForMetricItem((MetricItem)parent, tag, value);
/* 264 */     } else if (parent instanceof Config) {
/* 265 */       parseForConfig((Config)parent, tag, value);
/* 266 */     } else if (parent instanceof Condition) {
/* 267 */       parseForCondition((Condition)parent, tag, value);
/* 268 */     } else if (parent instanceof SubCondition) {
/* 269 */       parseForSubCondition((SubCondition)parent, tag, value);
    } else {
/* 271 */       throw new RuntimeException(String.format("Unknown tag(%s) under %s!", new Object[] { tag, parent }));
    }
  }
  
  private void parse(JsonReader reader) throws IOException {
    try {
/* 277 */       reader.parse(this);
/* 278 */     } catch (EOFException e) {
/* 279 */       if (this.m_objs.size() > 1) {
/* 280 */         throw new EOFException(String.format("Unexpected end while parsing json! tags: %s.", new Object[] { this.m_tags }));
      }
    }
    
/* 284 */     this.m_linker.finish();
  }
  
  public void parseForCondition(Condition condition, String tag, String value) {
/* 288 */     if (!"sub-conditions".equals(tag))
    {
/* 290 */       if ("minute".equals(tag)) {
/* 291 */         condition.setMinute((Integer)convert(Integer.class, value, null));
/* 292 */       } else if ("title".equals(tag)) {
/* 293 */         condition.setTitle(value);
/* 294 */       } else if ("alertType".equals(tag)) {
/* 295 */         condition.setAlertType(value);
      } else {
/* 297 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, condition, this.m_tags }));
      }  }
  }
  
  public void parseForConfig(Config config, String tag, String value) {
/* 302 */     if (!"conditions".equals(tag))
    {
/* 304 */       if ("starttime".equals(tag)) {
/* 305 */         config.setStarttime(value);
/* 306 */       } else if ("endtime".equals(tag)) {
/* 307 */         config.setEndtime(value);
      } else {
/* 309 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, config, this.m_tags }));
      }  }
  }
  
  public void parseForMetricItem(MetricItem metricItem, String tag, String value) {
/* 314 */     if ("monitorCount".equals(tag)) {
/* 315 */       metricItem.setMonitorCount((Boolean)convert(Boolean.class, value, null));
/* 316 */     } else if ("monitorSum".equals(tag)) {
/* 317 */       metricItem.setMonitorSum((Boolean)convert(Boolean.class, value, null));
/* 318 */     } else if ("monitorAvg".equals(tag)) {
/* 319 */       metricItem.setMonitorAvg((Boolean)convert(Boolean.class, value, null));
/* 320 */     } else if ("metricItemText".equals(tag)) {
/* 321 */       metricItem.setMetricItemText(value);
/* 322 */     } else if ("productText".equals(tag)) {
/* 323 */       metricItem.setProductText(value);
    } else {
/* 325 */       throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, metricItem, this.m_tags }));
    }
  }
  
  public void parseForMonitorRules(MonitorRules monitorRules, String tag, String value) {
/* 330 */     if ("rules".equals(tag)) {
      return;
    }
/* 333 */     throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, monitorRules, this.m_tags }));
  }

  
  public void parseForRule(Rule rule, String tag, String value) {
/* 338 */     if (!"metric-items".equals(tag) && !"configs".equals(tag))
    {
/* 340 */       if ("id".equals(tag)) {
/* 341 */         rule.setId(value);
      } else {
/* 343 */         rule.setDynamicAttribute(tag, value);
      }  }
  }
  
  public void parseForSubCondition(SubCondition subCondition, String tag, String value) {
/* 348 */     if ("type".equals(tag)) {
/* 349 */       subCondition.setType(value);
/* 350 */     } else if ("text".equals(tag)) {
/* 351 */       subCondition.setText(value);
    } else {
/* 353 */       throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, subCondition, this.m_tags }));
    }
  }
  
  static class JsonReader { private Reader m_reader;
    private char[] m_buffer;
    
    public JsonReader(Reader reader) {
/* 361 */       this.m_buffer = new char[2048];






/* 368 */       this.m_reader = reader;
    }
    private int m_size; private int m_index;
    private char next() throws IOException {
/* 372 */       if (this.m_index >= this.m_size) {
/* 373 */         this.m_size = this.m_reader.read(this.m_buffer);
/* 374 */         this.m_index = 0;

/* 376 */         if (this.m_size == -1) {
/* 377 */           throw new EOFException();
        }
      }
      
/* 381 */       return this.m_buffer[this.m_index++];
    }
    
    public void parse(DefaultJsonParser parser) throws IOException {
/* 385 */       StringBuilder sb = new StringBuilder();
/* 386 */       boolean flag = false;
      
      while (true) {
/* 389 */         char ch = next();

/* 391 */         switch (ch) {
          case '\t':
          case '\n':
          case '\r':
          case ' ':
            continue;
          case '{':
/* 398 */             parser.onObjectBegin();
/* 399 */             flag = false;
            continue;
          case '}':
/* 402 */             if (flag) {
/* 403 */               parser.onValue(sb.toString());
/* 404 */               sb.setLength(0);
            }
            
/* 407 */             parser.onObjectEnd();
/* 408 */             flag = false;
            continue;
          case '"':
          case '\'':
            while (true) {
/* 413 */               char ch2 = next();

/* 415 */               if (ch2 != ch) {
/* 416 */                 if (ch2 == '\\') {
/* 417 */                   char ch3 = next();

/* 419 */                   switch (ch3) {
                    case 't':
/* 421 */                       sb.append('\t');
                      continue;
                    case 'r':
/* 424 */                       sb.append('\r');
                      continue;
                    case 'n':
/* 427 */                       sb.append('\n');
                      continue;
                  }
/* 430 */                   sb.append(ch3);
                  
                  continue;
                }
/* 434 */                 sb.append(ch2); continue;
              }  break;
            }
/* 437 */             if (!flag) {
/* 438 */               parser.onName(sb.toString());
            } else {
/* 440 */               parser.onValue(sb.toString());
/* 441 */               flag = false;
            }
            
/* 444 */             sb.setLength(0);
            continue;



          
          case ':':
/* 451 */             if (sb.length() != 0) {
/* 452 */               parser.onName(sb.toString());
/* 453 */               sb.setLength(0);
            }
            
/* 456 */             flag = true;
            continue;
          case ',':
/* 459 */             if (sb.length() != 0) {
/* 460 */               if (!flag) {
/* 461 */                 parser.onName(sb.toString());
              } else {
/* 463 */                 parser.onValue(sb.toString());
              }
              
/* 466 */               sb.setLength(0);
            }
            
/* 469 */             flag = false;
            continue;
          case '[':
/* 472 */             parser.onArrayBegin();
/* 473 */             flag = false;
            continue;
          case ']':
/* 476 */             parser.onArrayEnd();
/* 477 */             flag = false;
            continue;
        }
/* 480 */         sb.append(ch);
      }
    } }

}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/rule/transform/DefaultJsonParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */